<template>
    <el-dialog v-model="localVisible" title="描述详情" @close="handleCancel">
        <el-collapse v-model="activeKey">
            <el-collapse-item v-for="(item, index) in detail" :title="item.problem" :name="index" :key="index">
                <div class="detailBox">
                    <div class="item">
                        <div class="lab">文本：</div>
                        <div class="val">{{ item.problem }}</div>
                    </div>
                    <div class="item">
                        <div class="lab">照片：</div>
                        <div class="val">
                            <img :src="getImgUrl(item.fileName)" class="val-img" />
                        </div>
                    </div>
                    <div class="item">
                        <div class="lab">视频：</div>
                        <div class="val">
                            <video controls :width="200" :src="getImgUrl(item.pictureName)" />
                        </div>
                    </div>
                </div>
            </el-collapse-item>
        </el-collapse>
    </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue';
import { ElDialog, ElCollapse, ElCollapseItem } from 'element-plus';
import { getFileAccessHttpUrl } from '@/api/manage';

const props = defineProps({
    visible: {
        type: Boolean,
        default: false
    },
    initial: {
        type: Array,
        default: () => ([])
    }
});

const emit = defineEmits(['cancel']);

const localVisible = ref(props.visible);
const detail = ref([...props.initial]);
const activeKey = ref(['0']);

watch(() => props.visible, (newVal) => {
    localVisible.value = newVal;
    if (newVal) {
        detail.value = [...props.initial];
    }
});

watch(localVisible, (newVal) => {
    if (!newVal) {
        emit('cancel');
    }
});

const handleCancel = () => {
    localVisible.value = false;
};

const getImgUrl = (imgName, type) => {
    if (imgName && imgName.split(',')) {
        type = type ? type + '/' : '';
        return getFileAccessHttpUrl('systemConfig/static/' + type + imgName.split(',')[0]);
    } else {
        return '';
    }
};
</script>

<style lang="less" scoped>
.detailBox {
    .item {
        display: flex;
        justify-content: flex-start;
        line-height: 24px;
        margin-bottom: 12px;

        .lab {
            width: 80px;
            color: #999;
            text-align: right;
            padding-right: 10px;
            box-sizing: border-box;
        }

        .val {
            flex: 1;

            .val-img {
                display: block;
                max-width: 100%;
            }
        }
    }
}
</style>